#ifndef COLORPICKER_H
#define COLORPICKER_H

#include <QWidget>
#include <QImage>
#include <QPointF>

class QPointF;
struct Vertex;

class ColorPicker : public QWidget
{
    Q_OBJECT
    Q_PROPERTY( QColor color READ color WRITE setColor)
    Q_PROPERTY( Qt::AlignmentFlag alignment READ alignment WRITE setAlignment)

public:
    ColorPicker(QWidget *parent = 0);
    ~ColorPicker();

    QSize sizeHint() const;
    QSize minimumSizeHint() const;
    void leaveEvent(QEvent *);
    void enterEvent(QEvent *);

    int heightForWidth(int w) const;

    void polish();

    QColor color() const;

    Qt::AlignmentFlag alignment() const;
    void setAlignment(Qt::AlignmentFlag value);


signals:
    void colorChanged(const QColor &col);

public slots:
    void setColor(const QColor &col);

protected:
    void paintEvent(QPaintEvent *);
    void mouseMoveEvent(QMouseEvent *);
    void mousePressEvent(QMouseEvent *);
    void mouseReleaseEvent(QMouseEvent *);
    void keyPressEvent(QKeyEvent *e);
    void resizeEvent(QResizeEvent *);
    void drawTrigon(QImage *p, const QPointF &a, const QPointF &b,
            const QPointF &c, const QColor &color);

private:
    double radiusAt(const QPointF &pos, const QRect &rect) const;
    double angleAt(const QPointF &pos, const QRect &rect) const;
    QPointF movePointToTriangle(double x, double y, const Vertex &a,
                    const Vertex &b, const Vertex &c) const;

    QPointF pointFromColor(const QColor &col) const;
    QColor colorFromPoint(const QPointF &p) const;

    void genBackground();

    Qt::AlignmentFlag _alignement;

    QImage bg;
    double a, b, c;
    QPointF pa, pb, pc, pd;

    QColor curColor;
    int curHue;

    bool mustGenerateBackground;
    qreal penWidth;
    int ellipseSize;

    int outerRadius;
    QPointF selectorPos;

    enum SelectionMode {
    Idle,
    SelectingHue,
    SelectingSatValue
    } selMode;
};

#endif
